package com.leetcode.No0997;

import java.util.HashMap;

/**
 * @program: leetcode
 * @description: 找到小镇的法官
 * @author: wangzhihua
 * @date: 2021-12-19
 */
public class Solution01 {

	public int findJudge(int n, int[][] trust) {
		int[] trustOther = new int[n + 1];
		int[] beTrusted = new int[n + 1];

		for (int[] t : trust) {
			++trustOther[t[0]];
			++beTrusted[t[1]];
		}

		int ans = -1;
		for (int i = 1; i <= n; ++i) {
			if (trustOther[i] == 0 && beTrusted[i] == n - 1) {
				if (ans == -1) {
					ans = i;
				} else {
					return -1;
				}
			}
		}

		return ans;
	}

}
